package com.example.taobaounion.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

import com.example.taobaounion.R;
import com.example.taobaounion.base.BaseFragment;
import com.example.taobaounion.model.bean.Categories;
import com.example.taobaounion.presenter.IHomePresenter;
import com.example.taobaounion.ui.activity.IMainActivity;
import com.example.taobaounion.ui.activity.ScanQrCodeActivity;
import com.example.taobaounion.ui.adapter.HomePagerAdapter;
import com.example.taobaounion.utils.LogUtils;
import com.example.taobaounion.utils.PresenterManager;
import com.example.taobaounion.view.IHomeCallback;
import com.google.android.material.tabs.TabLayout;

import butterknife.BindView;

public class HomeFragment extends BaseFragment implements IHomeCallback {


    private IHomePresenter mHomePresenter;
    //private HomePresenterImpl mHomePresenter;
    @BindView(R.id.home_indicator)
    public TabLayout mTabLayout;
    @BindView(R.id.home_pager)
    public ViewPager homeViewPager;
    @BindView(R.id.home_search_input_box)
    public View mSearchInputBox;
    @BindView(R.id.scan_icon)
    public View scan_icon;


    private HomePagerAdapter mHomePagerAdapter;


    @Override
    protected int getRootViewResId() {
        return R.layout.fragment_home;
    }

    @Override
    public View onCreateView(@NonNull  LayoutInflater inflater, @Nullable  ViewGroup container, @Nullable Bundle savedInstanceState) {
        LogUtils.d(this,"onCreateView.......");
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        LogUtils.d(this,"onDestroyView.......");
        super.onDestroyView();
    }

    @Override
    protected void loadData() {
        //加载数据
        mHomePresenter.getCategories();//一般点进去是不给看实现，看接口，所以换成IHomePresenter类型
    }

    @Override
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.base_home_fragment_layout,container,false);
    }

    @Override
    protected void initView(View rootView) {
        //tabLayout绑定viewPaper
        mTabLayout.setupWithViewPager(homeViewPager);
        //给viewPager设置适配器
        //在fragment里获取FragmentManager用getChildFragmentManager
        mHomePagerAdapter = new HomePagerAdapter(getChildFragmentManager());
        homeViewPager.setAdapter(mHomePagerAdapter);

    }

    @Override
    protected void initListener() {
        //搜索框点击
        mSearchInputBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到搜索界面
                FragmentActivity activity = getActivity();//任何一个Fragment都能获取他依附的Activity也就是MainActivity
                //想当与多态：FragmentActivity activity=new MainActivity();
                //如果activity是MainActivity类型的实例，返回true,activity的编译类型是FragmentActivity,与
                //MainActivity和IMainActivity具有父子关系，所以可以进行instanceof
                if(activity instanceof IMainActivity){
                    //大转小，转成子类MainActivity的实例
                    ((IMainActivity)activity).switch2SearchPage();//因为子类独有，父类没有需要强转
                }
            }
        });

        //扫码框点击
        scan_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到扫码界面
                startActivity(new Intent(getContext(), ScanQrCodeActivity.class));
            }
        });
    }

    @Override
    protected void initPresenter() {
        //创建Presenter
        mHomePresenter = PresenterManager.getInstance().getHomePresenter();
        mHomePresenter.registerViewCallback(this);
//        FragmentActivity activity = getActivity();
//        if(activity instanceof MainActivity){
//            LogUtils.d(this,"text");
//            LogUtils.d(this,"这是一个MainActivity.....");
//        }
    }


    @Override
    public void onCategoriesLoaded(Categories categories) {
        setUpState(State.SUCCESS);
        LogUtils.d(this,"onCategoriesLoaded...");
        //加载的数据从这里回来，将数据显示到UI
        if(mHomePagerAdapter!=null){
            mHomePagerAdapter.setCategories(categories);
            //homeViewPager.setOffscreenPageLimit(1);这个可以设置预加载的页面，如果是categories.getData().size
            //则预加载全部，会浪费资源，本项目没设置，使用默认的预加载也就是默认前面自己后面加载
        }
    }

    @Override
    public void onError() {
        setUpState(State.ERROR);
    }

    @Override
    public void onLoading() {
       setUpState(State.LOADING);
    }

    @Override
    public void onEmpty() {
       setUpState(State.EMPTY);
    }

    @Override
    protected void release() {
        //取消回调注册
       if(mHomePresenter!=null){
           mHomePresenter.unregisterViewCallback(this);
       }
    }

    @Override
    protected void onRetryClick() {
        //网络错误，点击了重试
        //重新加载分类内容
        if(mHomePresenter!=null){
            mHomePresenter.getCategories();
        }
    }
}
